package com.cloudshop.service.mongo.mq;

import com.cloudshop.service.common.log.GlobalAccessLog;
import com.cloudshop.service.mongo.dao.LogEntityMongoRepository;
import com.cloudshop.service.mongo.entity.LogEntity;
import javax.annotation.Resource;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

/**
 * 请求日志mq消费者.
 *
 * @author 宫新程
 * @since 2019/3/30 1:41
 */
@Component
@RabbitListener(bindings = @QueueBinding(exchange = @Exchange("cloud"), value = @Queue("log")))
public class LogMqConsumer {

  @Resource private LogEntityMongoRepository logEntityMongoRepository;

  /** 请求日志mq消费者. */
  @RabbitHandler
  public void logMqConsumer(GlobalAccessLog log) {
    LogEntity logEntity = new LogEntity();
    BeanUtils.copyProperties(log, logEntity);
    logEntityMongoRepository.save(logEntity);
  }
}
